package com.aoxon.cargo.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.Supplier;
import com.aoxon.cargo.loader.FileLoader;
import com.aoxon.cargo.shared.DialShared;
import com.aoxon.cargo.state.DataState;

public class PurDialsListAdapter extends BaseAdapter implements MyAdapter {
	private String tag="PurDialsListAdapter";
	
	private Activity activity;

	private boolean isBusy = false;
	private List<Supplier> suppliers;
	private LayoutInflater layoutInflater;

	private FileLoader myImageLoader = new FileLoader(100, 100, false);

	public PurDialsListAdapter(Activity activity, List<Supplier> suppliers) {
		this.suppliers = suppliers;
		this.activity = activity;
		this.layoutInflater = LayoutInflater.from(activity.getBaseContext());
	}

	@SuppressWarnings("unchecked")
	@Override
	public void resetList(List<?> list) {
		// TODO Auto-generated method stub
		if (list != null) {
			this.suppliers.clear();
			this.suppliers = (List<Supplier>) list;
		}
		this.notifyDataSetChanged();
	}

	@Override
	public void setBusy(boolean isBusy) {
		// TODO Auto-generated method stub
		this.isBusy = isBusy;
	}

	@Override
	public void clearCache() {
		// TODO Auto-generated method stub
		if (myImageLoader != null) {
			myImageLoader.clearCache();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return suppliers.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return suppliers.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View converView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		Log.v(tag, "position"+position);
		if (converView == null) {
			viewHolder = new ViewHolder();
			converView = layoutInflater.inflate(R.layout.pur_dials_list_item,
					null);
//			viewHolder.ivSupplierLogo = (ImageView) converView
//					.findViewById(R.id.ivPurDialListItemLogo);
			viewHolder.ivCertificate = (ImageView) converView
					.findViewById(R.id.ivPurDialListItemCertification);
			viewHolder.tvStoreName = (TextView) converView
					.findViewById(R.id.tvPurDialListItemStoreName);
			viewHolder.tvCompanyName = (TextView) converView
					.findViewById(R.id.tvPurDialListItemCompanyName);
			viewHolder.tvTime = (TextView) converView
					.findViewById(R.id.tvPurDialListItemTime);
			viewHolder.tvPhone = (TextView) converView
					.findViewById(R.id.tvPurDialListItemPhone);
//			viewHolder.progressBar = (ProgressBar) converView
//					.findViewById(R.id.pbPurDialListItemLogoLoading);

			converView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) converView.getTag();
		}

		try {

			final Supplier supplier = suppliers.get(getCount() - position - 1);
//			final Supplier supplier = suppliers.get(position);
//			viewHolder.ivSupplierLogo.setImageResource(R.drawable.loading);
			viewHolder.tvStoreName.setText(supplier.getStoreName());
			viewHolder.tvCompanyName.setText(supplier.getSupCompanyFullName());
			viewHolder.tvTime.setText(supplier.getSaveTime());
			viewHolder.tvPhone.setText(supplier.getSupPhone());

			viewHolder.tvPhone.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (supplier != null) {
						DialShared.saveDialRecord(supplier);
						suppliers = DialShared.getDialList();
						notifyDataSetChanged();
						Intent intent = new Intent(Intent.ACTION_CALL, Uri
								.parse("tel:" + supplier.getSupPhone()));
						activity.startActivity(intent);
					}
				}
			});

			if (supplier.getCerState() == DataState.SUCCESS) {
				viewHolder.ivCertificate.setVisibility(View.VISIBLE);
			} else {
				viewHolder.ivCertificate.setVisibility(View.GONE);
			}

//			String path = supplier.getSupLogo().get(0).getPictureUrl()
//					+ "s.png";
//			if (!isBusy) {
//				myImageLoader.displayImage(path, viewHolder.ivSupplierLogo,
//						viewHolder.progressBar, false);
//			} else {
//				myImageLoader.displayImage(path, viewHolder.ivSupplierLogo,
//						viewHolder.progressBar, true);
//			}

		} catch (Exception e) {
			e.printStackTrace();
//			viewHolder.progressBar.setVisibility(View.GONE);
		}

		return converView;
	}

	class ViewHolder {
//		ProgressBar progressBar;
//		ImageView ivSupplierLogo;
		ImageView ivCertificate;
		TextView tvCompanyName;
		TextView tvStoreName;
		TextView tvPhone;
		TextView tvTime;
	}

}
